/******************************************************************************/
//	EventFactory.cpp	-	Handles creation and deletion of events
/******************************************************************************/

#include "EventFactory.h"
#include "windows.h"

EventFactory	eventFactory;

EventFactory::EventFactory()
{
}

EventFactory::~EventFactory()
{
	// TODO: Delete all remaining inventory, pretty straight forward.
	for(unsigned i = 0; i < m_inventory.size();i++)
	{
		//m_inventory[i]->s_nEventsDeleted++;
		delete m_inventory[i];
	}

	if(Event::s_nEventsDeleted != Event::s_nEventsNewed)
		MessageBox(NULL, "Events still in memory!", "ERROR! Memory Leak", MB_ICONWARNING);
}

Event* EventFactory::GetEvent()
{
	Event *data = NULL;
		// TODO: Return an unused event to the caller.
		//		 If we do not have any available, return a new Event. 
		//		 Otherwise, return an existing one(Make sure it's re-initialized as if it's a new Event).
		// Note: Every event that we created so far is either contained here in the factory and waiting to be "used"
		//				or it's being used by the state machine and therefore is not contained here.
	if(m_inventory.size() > 0)
	{
		data = m_inventory.front();
		data->Set(data->EVENT_ID_UNDEFINED);
		m_inventory.erase(m_inventory.begin());
	}
	if(data == NULL)
		data = new Event();
	//data->s_nEventsNewed++;
	//data->i
	// Return the event to user
	return data;
}

void EventFactory::ReturnEvent(Event* data)
{
	// TODO: Store the event that is being returned by the user. 
	// Why? Because the caller of this function is done with the event and now 
	// it's ok to let someone else reuse the memory for another event.
	m_inventory.push_back(data);
}
